#include "pch.h"
#include "CIO.h"
#include "SP.h"
CIO::CIO()
{
	
}
CIO::~CIO()
{
}

void CIO::GetAllInputIo()
{
	int nErr = 0;
	Manager.m_wDIVal[0] = dmc_read_inport(Manager.m_Card, 0);
	nmc_read_inport(Manager.m_Card, 1, 0, &Manager.m_wDIVal[1]);
	return;
}
void CIO::GetOutPutIO()
{
	Y00 = (((Manager.m_wDOVal[0] & 0x0001) == 0x0001) ? OFF : ON);
	Y01 = (((Manager.m_wDOVal[0] & 0x0002) == 0x0002) ? OFF : ON);
	Y02 = (((Manager.m_wDOVal[0] & 0x0004) == 0x0004) ? OFF : ON);
	Y03 = (((Manager.m_wDOVal[0] & 0x0008) == 0x0008) ? OFF : ON);
	Y04 = (((Manager.m_wDOVal[0] & 0x0010) == 0x0010) ? OFF : ON);
	Y05 = (((Manager.m_wDOVal[0] & 0x0020) == 0x0020) ? OFF : ON);
	Y06 = (((Manager.m_wDOVal[0] & 0x0040) == 0x0040) ? OFF : ON);
	Y07 = (((Manager.m_wDOVal[0] & 0x0080) == 0x0080) ? OFF : ON);
	Y08 = (((Manager.m_wDOVal[0] & 0x0100) == 0x0100) ? OFF : ON);
	Y09 = (((Manager.m_wDOVal[0] & 0x0200) == 0x0200) ? OFF : ON);
	Y10 = (((Manager.m_wDOVal[0] & 0x0400) == 0x0400) ? OFF : ON);
	Y11 = (((Manager.m_wDOVal[0] & 0x0800) == 0x0800) ? OFF : ON);
	Y12 = (((Manager.m_wDOVal[0] & 0x1000) == 0x1000) ? OFF : ON);
	Y13 = (((Manager.m_wDOVal[0] & 0x2000) == 0x2000) ? OFF : ON);
	Y14 = (((Manager.m_wDOVal[0] & 0x4000) == 0x4000) ? OFF : ON);
	Y15 = (((Manager.m_wDOVal[0] & 0x8000) == 0x8000) ? OFF : ON);
	Y16 = (((Manager.m_wDOVal[1] & 0x0001) == 0x0001) ? OFF : ON);
	Y17 = (((Manager.m_wDOVal[1] & 0x0002) == 0x0002) ? OFF : ON);


	return;
}
void CIO::SetOutPutIO(int nPort, long nValue)
{
	int nCard = nPort /16;
	if (nValue == OFF)
	{
		switch (nCard)
		{
		case 0:	dmc_write_outbit(Manager.m_Card, (nPort % 16), ON); break;

		case 1:	nmc_write_outbit(Manager.m_Card, 1, (nPort % 16), ON); break;
		}
	}
	else if (nValue == ON)
	{
		switch (nCard)
		{
		case 0:	dmc_write_outbit(Manager.m_Card, (nPort % 16), OFF); break;

		case 1:	nmc_write_outbit(Manager.m_Card, 1, (nPort % 16), OFF); break;
		}
	}
	Manager.m_wDOVal[0] = dmc_read_outport(Manager.m_Card, 0);
	nmc_read_outport(Manager.m_Card, 1, 0, &Manager.m_wDOVal[1]);
	GetOutPutIO();

}
unsigned int __stdcall GetIoInputThread(void *arg)
{
	CIO* cio = (CIO*)arg;
	while (0 == cio->m_nExitFlagIo)
	{
		Sleep(10);
		cio->GetAllInputIo();
		cio->S00 = (((Manager.m_wDIVal[0] & 0x0001) == 0x00) ? ON : OFF);
		cio->S01 = (((Manager.m_wDIVal[0] & 0x0002) == 0x00) ? ON : OFF);
		cio->S02 = (((Manager.m_wDIVal[0] & 0x0004) == 0x00) ? ON : OFF);
		cio->S03 = (((Manager.m_wDIVal[0] & 0x0008) == 0x00) ? ON : OFF);
		cio->S04 = (((Manager.m_wDIVal[0] & 0x0010) == 0x00) ? ON : OFF);
		cio->S05 = (((Manager.m_wDIVal[0] & 0x0020) == 0x00) ? ON : OFF);
		cio->S06 = (((Manager.m_wDIVal[0] & 0x0040) == 0x00) ? ON : OFF);
		cio->S07 = (((Manager.m_wDIVal[0] & 0x0080) == 0x00) ? ON : OFF);
		cio->S08 = (((Manager.m_wDIVal[0] & 0x0100) == 0x00) ? ON : OFF);
		cio->S09 = (((Manager.m_wDIVal[0] & 0x0200) == 0x00) ? ON : OFF);
		cio->S10 = (((Manager.m_wDIVal[0] & 0x0400) == 0x00) ? ON : OFF);
		cio->S11 = (((Manager.m_wDIVal[0] & 0x0800) == 0x00) ? ON : OFF);
		cio->S12 = (((Manager.m_wDIVal[0] & 0x1000) == 0x00) ? ON : OFF);
		cio->S13 = (((Manager.m_wDIVal[0] & 0x2000) == 0x00) ? ON : OFF);
		cio->S14 = (((Manager.m_wDIVal[0] & 0x4000) == 0x00) ? ON : OFF);
		cio->S15 = (((Manager.m_wDIVal[0] & 0x8000) == 0x00) ? ON : OFF);
	}
	
	return 0;
}
void CIO::StartGetIOInputThread()
{
	unsigned int threadid;
	HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, GetIoInputThread, (LPVOID)this, 0, &threadid);
	if (hThread == NULL)
	{
		return;
	}
	m_Iothread.push_back(hThread);
}


void CIO::ShotDown()
{
	m_nExitFlagIo = 1;

	auto sz = m_Iothread.size();
	if (sz > 0)
	{
		::WaitForMultipleObjects((DWORD)sz, &m_Iothread[0], TRUE, INFINITE);
	}
	m_Iothread.clear();

}

void CIO::SetAllOutPutOff()
{
	CString cstrLog;
	int nErr = 0;
	Manager.m_wDOVal[0] = 0xffff;
	Manager.m_wDOVal[1] = 0xffff;
	Manager.GetLog()->DisplayAndSaveLog(L"SetAllOutputOff Success!", LV_EVENT);
	return;
}


